angular.module('app.service', ['ngResource', 'ngCookies'])

//market
    .service('MarketService', ['$resource', function ($resource) {
        //增加device
        this.add = function (device, success, error) {
            var resource = $resource(testurl + "market");
            return resource.save({}, device, success, error);
        }
        //通过id,获取device
        this.getbyid = function (id) {
            var resource = $resource(testurl + "market/:id");
            return resource.get({id: id});
        }
        //删除device
        this.deletebyid = function (id) {
            var resource = $resource(testurl + "market/:id");
            return resource.remove({id: id});
        }
        //更新device
        this.updatebyid = function (id, device) {
            var resource = $resource(testurl + "market/:id", {}, {update: {method: "PUT"}});
            return resource.update({id: id}, device);
        }
        //通过获取所有device
        this.getbyall = function () {
            var resource = $resource(testurl + "market");
            return resource.query();
        }

    }])

//ad
    .service('AdvService', ['$resource', function ($resource) {
        //增加adv
        this.add = function (adv, success, error) {
            var resource = $resource(testurl + "adv");
            return resource.save({}, adv, success, error);
        }
        //通过id,获取adv
        this.getbyid = function (id) {
            var resource = $resource(testurl + "adv/:id");
            return resource.get({id: id});
        }
        //删除adv
        this.deletebyid = function (id) {
            var resource = $resource(testurl + "adv/:id");
            return resource.remove({id: id});
        }
        //更新adv
        this.updatebyid = function (id, adv) {
            var resource = $resource(testurl + "adv/:id", {}, {update: {method: "PUT"}});
            return resource.update({id: id}, adv);
        }
        //通过获取所有adv
        this.getbyall = function () {
            var resource = $resource(testurl + "adv");
            return resource.query();
        }

    }])

//relation
    .service('RelationService', ['$resource', function ($resource) {
        //
        this.add = function (relation, success, error) {
            var resource = $resource(testurl + "relation");
            return resource.save({}, relation, success, error);
        }
        //通过mid,get
        this.getbymid = function (mid) {
            var resource = $resource(testurl + "relation/mid/:mid");
            return resource.query({mid: mid});
        }
        //通过aid,get
        this.getbyaid = function (aid) {
            var resource = $resource(testurl + "relation/aid/:aid");
            return resource.query({aid: aid});
        }
        //删除advcontent
        this.deletebyid = function (mid, aid) {
            var resource = $resource(testurl + "relation/:mid/:aid");
            return resource.remove({mid: mid, aid: aid});
        }
        //更新
        this.updatebyid = function (mid,aid, relation) {
            var resource = $resource(testurl + "relation/:mid/:aid", {}, {update: {method: "PUT"}});
            return resource.update({mid: mid, aid: aid}, relation);
        }
        this.getoutdate=function(){
            var resource = $resource(testurl + "relation/outdated");
            return resource.query();
        }
    }])

    .service('fileUpload', ['$http','$q' ,function ($http,$q) {
        var uploadUrl="http://120.55.194.39:8183/mealplan/picupload"
        this.uploadFileToUrl = function(file){
            var fd = new FormData();
            fd.append('file', file);
            var deferred = $q.defer();
            $http({
                method: "POST",
                url: uploadUrl,
                data: fd,
                headers: {'Content-Type': undefined},
                transformRequest: angular.identity
            }).success(function(data,status,headers,config){
                deferred.resolve(data);
            }).error(function(data,status,headers,config){
                deferred.reject(data);
            });
            return deferred.promise;
        }
    }]);
;
